package com.apigen.platform.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;

/**
 * Spring Security配置
 *
 * @author API Generator
 * @since 2024-09-24
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            // 禁用CSRF保护
            .csrf().disable()
            // 禁用Session，使用无状态JWT
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            // 配置授权规则
            .authorizeHttpRequests()
                // 允许访问Knife4j文档相关路径
                .antMatchers("/doc.html**", "/webjars/**", "/swagger-resources/**", 
                           "/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html**", "/favicon.ico").permitAll()
                // 允许访问测试接口
                .antMatchers("/api/test/**").permitAll()
                // 允许访问Druid监控
                .antMatchers("/druid/**").permitAll()
                // 允许访问健康检查
                .antMatchers("/actuator/**").permitAll()
                // 暂时允许所有API访问（开发阶段）
                .antMatchers("/api/**").permitAll()
                // 其他请求需要认证
                .anyRequest().authenticated();

        return http.build();
    }
}
